﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace code_helper
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        
        bool readandwrite_cFile(string path)
        {
            Encoding fineEncoding = EncodingType.GetType(path);
            string srcString = System.IO.File.ReadAllText(path, fineEncoding);

            string targetString = codeWFL.codeDeleteNote(srcString, textBox2.Text);

            if (srcString == targetString)
                return false;

            try
            {
                System.IO.File.WriteAllText(path, targetString, System.Text.Encoding.Default);
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
            return true ;
        }
        private void handleAfile(string path)
        {
            textBox1.Text = path + "\r\n\r\n--------------------file change below--------------\r\n";
            if (readandwrite_cFile(path))
            {
                textBox1.Text += Path.GetFileName(path) + "\r\n";
            }
            textBox1.Text += "-------------------------The end-------------------";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            if (opf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                handleAfile(opf.FileName);
                return;
            }
        }
        private void handleAfolder(object args)
        {   
            string path = (string)args;
            this.Invoke(new MethodInvoker(delegate
            {
                textBox1.Text = path + "\r\n\r\n--------------------file change below--------------\r\n";
            }));
            string[] fiels = Directory.GetFiles(path, "*.c", SearchOption.AllDirectories);
            int count = 0;
            long starttick = DateTime.Now.Ticks;

            foreach (string fn in fiels)
            {
                if (readandwrite_cFile(fn))
                {
                    this.Invoke(new MethodInvoker(delegate
                    {
                        textBox1.Text += Path.GetFileName(fn) + "\r\n";
                    }));
                }
                this.Invoke(new MethodInvoker(delegate
                {
                    count++;
                    labelProgress.Text = count.ToString() + "/"+fiels.Length.ToString();
                    labelProgress.Text += "\r\n" + (count * 100 / fiels.Length).ToString() + "%";
                    labelProgress.Text += "\r\n用时：" +
                                            ((DateTime.Now.Ticks - starttick) / 10000000).ToString() + "." +
                                            ((DateTime.Now.Ticks - starttick) % 10000000).ToString() + "秒";
                }));
                //Thread.Sleep(5);
            }
            this.Invoke(new MethodInvoker(delegate
            {
                textBox1.Text += "-------------------------The end-------------------";
            }));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog openfolder = new FolderBrowserDialog();
            if (openfolder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                Thread threadUpload = new Thread(new ParameterizedThreadStart(handleAfolder));
                threadUpload.IsBackground = true;
                threadUpload.Start(openfolder.SelectedPath);

                return;
            }
        }
        //--------------------------------------------------------------------------
   
        private void textBox1_DragEnter(object sender, DragEventArgs e)
        {

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {

                e.Effect = DragDropEffects.Link;

            }

            else if (e.Data.GetDataPresent(DataFormats.Text))
            {

                e.Effect = DragDropEffects.Copy;

            }

        }
        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {

                string str = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();

                if (File.Exists(str))
                {
                    handleAfile(str);
                    return;
                }
                else if (Directory.Exists(str))
                {
                    Thread threadUpload = new Thread(new ParameterizedThreadStart(handleAfolder));
                    threadUpload.IsBackground = true;
                    threadUpload.Start(str);
                    return;
                }
                else
                    return;

            }

            else if (e.Data.GetDataPresent(DataFormats.Text))
            {

                textBox1.Text = (e.Data.GetData(DataFormats.Text)).ToString();

            }

        }

    //--------------------------------------------------------------------------------
    }
}
